<?php
/**
 * 贡献榜
 */
namespace Appapi\Controller;
use Common\Controller\HomebaseController;
class ContributeController extends HomebaseController {
	
	function index(){
		$uid=I("uid");
		$p=1;
		$page_nums=20;
		$start=($p-1)*$page_nums;
		$list=M("users_coinrecord")->field("uid,sum(totalcoin) as total")->where(" action in ('sendgift','sendbarrage') and touid='{$uid}'")->group("uid")->order("total desc")->limit($start,$page_nums)->select();
		foreach($list as $k=>$v){
			$list[$k]['userinfo']=getUserInfo($v['uid']);
		}
		
		$this->assign("uid",$uid);
		$this->assign("p",$p+1);
		$this->assign("list",$list);


		$this->display();		
	}
	
	function getmore(){
		$uid=I("uid");
		$p=I("page");
		$page_nums=20;
		$start=($p-1)*$page_nums;
		
		$list=M("users_coinrecord")->field("uid,sum(totalcoin) as total")->where(" action in ('sendgift','sendbarrage') and touid='{$uid}'")->group("uid")->order("total desc")->limit($start,$page_nums)->select();
		foreach($list as $k=>$v){
			$list[$k]['userinfo']=getUserInfo($v['uid']);
		}
		
		
		$nums=count($list);
		if($nums<$page_nums){
			$isscroll=0;
		}else{
			$isscroll=1;
		}

		$result=array(
			'data'=>$list,
			'nums'=>$nums,
			'start'=>$start,
			'isscroll'=>$isscroll,
		);
	 
		echo json_encode($result);
		exit;		
	}

    /**
     * 个人排行榜
     *
     */
	public function  order(){
        $uid=I("uid");
        $type=I("type");

        if($type=='week'){

            $nowtime=time();
            //当天0点
            //$today=date("Ymd",$nowtime);
            //$today_start=strtotime($today);
            //当天 23:59:59
            //$today_end=strtotime("{$today} + 1 day")-1;

            $w=date('w',$nowtime);
            //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
            $first=1;
            //周一
            $week=date('Y-m-d H:i:s',strtotime( date("Ymd")."-".($w ? $w - $first : 6).' days'));
            $week_start=strtotime( date("Ymd")."-".($w ? $w - $first : 6).' days');

            //本周结束日期
            //周天
            $week_end=strtotime("{$week} +1 week")-1;


            $list=M("users_coinrecord")->field("uid,sum(totalcoin) as total")->where(" action in ('sendgift','sendbarrage') and touid='{$uid}' and addtime>{$week_start} and addtime<{$week_end}")->group("uid")->order("total desc")->limit(0,20)->select();

            foreach($list as $k=>$v){
                $list[$k]['userinfo']=getUserInfo($v['uid']);
            }
        }else{
            $list=M("users_coinrecord")->field("uid,sum(totalcoin) as total")->where(" action in ('sendgift','sendbarrage') and touid='{$uid}'")->group("uid")->order("total desc")->limit(0,20)->select();
            foreach($list as $k=>$v){
                $list[$k]['userinfo']=getUserInfo($v['uid']);
            }
        }

        $this->assign("list",$list);

        $this->display();

    }

    /**
     * 主播收益排行榜
     */
	public function order2(){

        set_time_limit(0);

		$type=I("type");
		
		if($type=='week'){
			
			$nowtime=time();
			//当天0点
			//$today=date("Ymd",$nowtime);
			//$today_start=strtotime($today);
			//当天 23:59:59
			//$today_end=strtotime("{$today} + 1 day")-1;

			$w=date('w',$nowtime); 
			//获取本周开始日期，如果$w是0，则表示周日，减去 6 天 
			$first=1;
			//周一
			$week=date('Y-m-d H:i:s',strtotime( date("Ymd")."-".($w ? $w - $first : 6).' days')); 
			$week_start=strtotime( date("Ymd")."-".($w ? $w - $first : 6).' days'); 

			//本周结束日期 
			//周天
			$week_end=strtotime("{$week} +1 week")-1;

			$list=M("users_coinrecord")->field("touid,sum(totalcoin) as total")->where(" action in ('sendgift') and addtime>{$week_start} and addtime<{$week_end}")->group("touid")->order("total desc")->limit(0,20)->select();
          //   echo M("users_coinrecord")->getLastSql();
			
			foreach($list as $k=>$v){
                $list[$k]['total'] =$this->format_money( $list[$k]['total']);
				$list[$k]['userinfo']=getUserInfo($v['touid']);
			}
		}else if ($type == 'month'){
            $date = date("Y-m-d");
            $first = date('Y-m-01 00:00:00', strtotime($date));  //本月第一天
            $firstday = strtotime(date('Y-m-01 00:00:00', strtotime($date)));  //本月第一天
            $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"))); //本月最后一天
            $list=M("users_coinrecord")->field("touid,sum(totalcoin) as total")->where(" action in ('sendgift') and addtime>{$firstday} and addtime<{$lastday}")->group("touid")->order("total desc")->limit(0,20)->select();
            foreach($list as $k=>$v){
                $list[$k]['total'] =$this->format_money( $list[$k]['total']);
                $list[$k]['userinfo']=getUserInfo($v['touid']);
            }
        }else{
			$list=M("users_coinrecord")->field("touid,sum(totalcoin) as total")->where(" action in ('sendgift')")->group("touid")->order("total desc")->limit(0,20)->select();
			//echo M("users_coinrecord")->getLastSql();
			foreach($list as $k=>$v){
                $list[$k]['total'] =$this->format_money( $list[$k]['total']);
                $list[$k]['userinfo']=getUserInfo($v['touid']);
			}
		}
        echo json_encode(array("ret"=>200,'data'=>$list,'msg'=>'操作成功'));

		//$this->display();

	}


    /**
     * 格式化金额
     *
     * @param int $money
     * @param int $len
     * @param string $sign
     * @return string
     */
    function format_money($money, $len=0, $sign=''){
        $negative = $money > 0 ? '' : '-';
        $int_money = intval(abs($money));
        $len = intval(abs($len));
        $decimal = '';//小数
        if ($len > 0) {
            $decimal = '.'.substr(sprintf('%01.'.$len.'f', $money),-$len);
        }
        $tmp_money = strrev($int_money);
        $strlen = strlen($tmp_money);
        $format_money=0;
        for ($i = 3; $i < $strlen; $i += 3) {
            $format_money .= substr($tmp_money,0,3).',';
            $tmp_money = substr($tmp_money,3);
        }
        $format_money .= $tmp_money;
        $format_money = strrev($format_money);
        return $sign.$negative.$format_money.$decimal;
    }

}